let baseUrl = "http://localhost:8080/thunder/"


function setRequest(url, method, data) {

	let header = {
		"content-type": "application/json",
	}
	if ("login" != url) {
		header = {
			"content-type": "application/json",
			"token": "123"
		}
	}

	var promise = new Promise((resolve, reject) => {
		var that = this;
		uni.request({
			url: baseUrl + url,
			data,
			method,
			dataType: "json",
			header,
			success: function(res) {
				resolve(res);
			},
			fail: function(e) {
				reject("网络出错,连接失败！" + JSON.stringify(e));
			}
		});
	});
	return promise;
}

function setRequestForm(url, method, data) {
	header
	let header = {
		"content-type": 'application/x-www-form-urlencoded',
	}
	if ("login" != url) {
		header = {
			"content-type": "application/x-www-form-urlencoded",
			"token": "123"
		}
	}

	var promise = new Promise((resolve, reject) => {
		var that = this;
		uni.request({
			url: baseUrl + url,
			data,
			method,
			dataType: "application/x-www-form-urlencoded",
			header,
			success: function(res) {
				resolve(res);
			},
			fail: function(e) {
				reject("网络出错,连接失败！" + JSON.stringify(e));
			}
		});
	});
	return promise;
}

function getRequest(url, data) {
	return setRequest(url, "GET", data);
}

function postRequest(url, data) {
	return setRequest(url, "POST", data);
}

function putRequest(url, data) {
	return setRequest(url, "PUT", data);
}

function deleteRequest(url, data) {
	return setRequest(url, "DELETE", data);
}

function setRequestFormf(url, data) {
	return setRequestForm(url, "POST", data);
}

module.exports = {
	get: getRequest,
	post: postRequest,
	put: putRequest,
	delete: deleteRequest,
	postf: setRequestFormf
};
